#!/bin/bash

set -e

manifestName='default.xml'
groupName='caffe'

function usage() {
    echo "Usage: $( basename $0 ) [-m <manifestName> | -p <project> | -b <localBranch> | -n | -h]"
    echo "      用户manifestUrl初始化代码仓库"
    echo "      命令行参数:" 
    echo "      -m <manifestName> manifest文件名称，缺省${manifestName}" 
    echo "      -p <project> 代码仓库名称" 
    echo "      -g <groupName> 清单组名称，缺省${groupName}"
    echo "      -b <localBranch> 本地分支名称。如果需要继续开发，一定要定义"
    echo "      -n 不增加用户仓库"
    echo "      -h 输出本信息"
    exit 1
}   

manifestUrl="git@gitlab.tsingmicro.com:manifests/toolchain_manifests.git"
localBranch=''
userRepo=1

while getopts "m:p:g:b:nh" arg
do
    case $arg in
        g)
            groupName="$OPTARG"
            ;;
        m)
            manifestName="$OPTARG"
            ;;
        p)
            project="$OPTARG"
            ;;
        b)
            localBranch="$OPTARG"
            ;;
        n)
            userRepo=0
            ;;
        *)
            usage
            ;;
    esac
done

if [ -z "${project}" ]
then
    usage
fi

echo repo init -u "${manifestUrl}" -b master -g ${groupName} -m ${manifestName}
repo init -u "${manifestUrl}" -b master -g ${groupName} -m ${manifestName}

echo repo sync "${project}"
repo sync "${project}"

if [ ! -z "${localBranch}" ]
then
    echo repo start "${localBranch}" "${project}"
    repo start "${localBranch}" "${project}"
fi

if [[ "${project}" == "quantize" ]]
then
    # 建立data, models符号链接
    dp=$( realpath --relative-to . ${HOME}/data )
    myln.sh "${dp}" data
    mp=$( realpath --relative-to . ${HOME}/models )
    myln.sh "${mp}" models
    ep=$( realpath --relative-to . ${HOME}/example_quantize)
    myln.sh "${ep}" example
fi

if [ ${userRepo} -eq 1 ]
then
    # 同时建立一个liujinsong的远程仓库
    cd "${project}"
    echo git remote add ljs "http://gitlab.tsingmicro.com/liujinsong/${project}.git"
    git remote add ljs "http://gitlab.tsingmicro.com/liujinsong/${project}.git"
fi
